<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //   document.onkeydown = function () {
      //     console.log("按下");
      //   };
      //   document.onkeyup = function () {
      //     console.log("抬起");
      //   };
      //   document.onkeypress = function () {
      //     console.log("按压");
      //   };
      //   document.omkeydown = function (event) {
      //     var keycode = event.keyCode || event.which;
      //     // console.log(keycode);
      //     if (keycode == 65) {
      //       console.log("按了a键");
      //     } else if (keycode == 66) {
      //       console.log("按下了b键");
      //     }
      //   };
      document.onkeydown = function (event) {
        var key = event.key; //获取到的是 key值；
        // console.log(key);
        if (key == "a") {
          console.log("按了a键");
        } else if (key == "b") {
          console.log("按了b键");
        }
      };
    </script>
  </body>
</html>
